﻿using Sidvall.Serialization;
using System;
using System.Globalization;

namespace Sidvall.UI.Wpf.Converters
{
    public class DoubleToStringConverter : ValueToStringConverterBase
    {
        #region Public Members

        #region StringToValue

        protected override object StringToValue(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value.ToNullableDouble(NumberStyles.Any, culture);
        }

        #endregion
        #region ValueToString

        protected override string ValueToString(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (!(value is double))
                return null;
            var format = parameter.ToString(null);
            if (string.IsNullOrWhiteSpace(format))
                format = "r";
            return ((double)value).ToString(format, culture);
        }

        #endregion

        #endregion
    }
}
